//
//  NetworkTool.swift
//  weibopush
//
//  Created by 龚欣 on 15/6/25.
//  Copyright © 2015年 gong. All rights reserved.
//

import UIKit
import AFNetworking
class NetworkTool: AFHTTPSessionManager {

    static var instance : NetworkTool = {
        
        let baseURL = NSURL(string: "https://api.weibo.com/")
        let networktool = NetworkTool(baseURL: baseURL)
        networktool.responseSerializer.acceptableContentTypes = NSSet(objects: "application/json", "text/json", "text/javascript","text/plain") as! Set<String>

        return networktool
        }()
    class func sharedNetworkTool() -> NetworkTool {
        return instance
    }
    
    
    //  MARK: 发送微博
    func sendStatus(statusText : String,image : UIImage?,finished : (error : NSError?)->()){

        var params = accessToken()
        params["status"] = statusText
        if image == nil {
            let urlString = "2/statuses/update.json"
            POST(urlString, parameters: params, success: { (_, JSON) -> Void in
                finished(error: nil)
                }) { (_, error) -> Void in
                    print(error)
                    finished(error: error)
            }
        }else{
            let urlString = "2/statuses/upload.json"
            POST(urlString, parameters: params, constructingBodyWithBlock: { (formData) -> Void in
                let data = UIImagePNGRepresentation(image!)
                /**
                1. 文件的二进制数据
                2. 服务器的字段名，后端接口会提供
                3. 保存在服务器的文件名，有些服务器可以随便写(接收到请求后，会做后续处理)
                4. mimeType，告诉服务器文件类型，application/octect-stream
                */
                formData.appendPartWithFileData(data, name: "pic", fileName: "hello", mimeType: "application/octect-stream")
                
                }, success: { (_, JSON) -> Void in
                    finished(error: nil)
                }, failure: { (_, error) -> Void in
                    finished(error: error)
            })
        }
    }
    
    func loadData(since_id : Int, max_id : Int,finished : (array : [[String : AnyObject]]?,error : NSError?)->()){
        
        var para = accessToken()
        //新浪URL地址 https://api.weibo.com/2/statuses/home_timeline.json
        let URLString = "2/statuses/home_timeline.json"
        if since_id > 0{
            para["since_id"] = "\(since_id)"
        }
        if max_id > 0{
            para["since_id"] = "0"
            para["max_id"] = "\(max_id)"
        }
        
        NetworkTool.sharedNetworkTool().GET(URLString, parameters: para, success: { (_, JSON) -> Void in

            if let array = JSON["statuses"] as? [[String : AnyObject]]{
                finished(array: array, error: nil)
                return
            }
            finished(array: nil, error: nil)
            
            }) { (_, error) -> Void in
                print(error)
                finished(array: nil, error: nil)
        }
    }

    func accessToken() -> [String : String]{
        assert(sharedAccount != nil, "必须要先登录才能访问网络")
        return ["access_token" : sharedAccount!.access_token!]
    }
 
}
